package site.hugboy.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.hugboy.reggie.dto.DishDto;
import site.hugboy.reggie.entity.Dish;
import site.hugboy.reggie.entity.DishFlavor;
import site.hugboy.reggie.mapper.DishMapper;
import site.hugboy.reggie.service.DishFlavorService;
import site.hugboy.reggie.service.DishService;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFS;

    /**
     * 新增菜品 同时保存口味数据
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto){
        // 1. 保存菜品的基本信息到 菜品表dish
        this.save(dishDto);

        // 2. 保存菜品口味到  菜品口味表dish_flavor
        Long dishId = dishDto.getId();  // 菜品 id
        List<DishFlavor> flavors = dishDto.getFlavors();  // 菜品 口味
        // 追加 dishId 字段
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        dishFS.saveBatch(flavors);
    }
}
